Integrating and Developing with CookieBanner

Introduction

This guide explains how to integrate the CookieBanner's accept/decline mechanism into your own Paymenter extension. The CookieBanner extension sets a cookie (cookieConsent) when a user accepts or declines cookies. You can use this cookie to conditionally execute code in your extension based on the user's consent.

Prerequisites

Integration Steps

1. Create a New Extension

Start by creating a new extension in your Paymenter instance. Use the following template as a starting point:

<?php

namespace Paymenter\Extensions\Others\Example;

use App\Classes\Extension\Extension; use Illuminate\Support\Facades\Event; use Illuminate\Support\HtmlString;

class Example extends Extension { public function boot(): void { if (!request()->isMethod('GET')) { return; }

Event::listen('body', function () { return ['view' => new HtmlString($this->getScriptTemplate())]; }); }

private function getScriptTemplate(): string { return << document.addEventListener("DOMContentLoaded", function() { const cookieConsent = document.cookie.includes("cookieConsent=accepted") ? "accepted" : document.cookie.includes("cookieConsent=declined") ? "declined" : null;

if (cookieConsent === "accepted") { // Code to run when cookies are accepted console.log("Accepted: place your code here."); }

if (cookieConsent === "declined") { // Code to run when cookies are declined console.log("Declined: place your code here."); } }); HTML; }

public function enabled(): void {} public function disabled(): void {} public function updated(): void {} public function install(): void {} }

Understanding the Cookie Consent Check

How the Cookie Check Works

The JavaScript code checks for the presence of the cookieConsent cookie to determine the user's consent status. Here's how it works:

If it is, the user has accepted cookies. If it is, the user has declined cookies.

Using the Consent Status

Once the cookieConsent status is determined, you can use it to control the behavior of your website:

You can load scripts or enable features that require user consent, such as analytics, tracking, or personalized content. You should avoid loading scripts or enabling features that require cookies. Instead, respect the user's choice by disabling or hiding those features. The user has not yet made a choice, so the cookie banner should still be visible, and no cookie-dependent features should be enabled.

Practical Applications

Only load analytics scripts if the user has accepted cookies. This ensures compliance with privacy regulations and respects user preferences. If your site offers personalized content based on user data, only enable this feature if the user has accepted cookies. Many third-party services, such as chatbots or social media plugins, require cookies. Only load these services if the user has given their consent. If cookies are declined, you might want to inform the user that some features are unavailable. You can do this by displaying a message or modifying the page layout.

Best Practices

Clearly communicate to users what cookies are being used for and how their data will be handled. This builds trust and ensures compliance with privacy laws. Always respect the user's decision. If they decline cookies, avoid loading any scripts or features that rely on cookies. Test your implementation thoroughly to ensure that cookie-dependent features are correctly enabled or disabled based on user consent. Provide fallbacks or alternative functionality for users who decline cookies. This ensures that your site remains usable and accessible to all visitors.